package com.example.fence.controller;

import com.example.fence.entity.Fence;
import com.example.fence.service.impl.ElectricFenceServiceImpl;
import com.example.fence.util.FenceStoreUtil;
import com.example.fence.util.Result;
import com.example.fence.vo.FenceVo;
import lombok.extern.slf4j.Slf4j;

import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.example.fence.util.ResultCode.FAIL;
import static com.example.fence.util.ResultCode.SUCCESS;

/**
 * 接收前端发送的请求，调用service并将结果返回给前端
 */

@RestController
@RequestMapping("electricFence")
@CrossOrigin
@Slf4j
public class ElectricFenceController {
    @Autowired
    private ElectricFenceServiceImpl electricFenceServiceImpl;


    /**
     * 创建电子围栏，url:  server:port/electricFence/create
     * @param fenceVo 电子围栏信息
     * @return Result包装后的结果
     */
    @PostMapping("/create")
    @ResponseBody

    public Result createFence(@RequestBody FenceVo fenceVo) throws MqttException {

        // 整体上传了null
        if(fenceVo==null){
            log.warn("FenceVo is null!");
            return new Result(FAIL.code, false, null,"Fence info is not existed!");
        }
        // 路径未上传
        if(fenceVo.getPath()==null||fenceVo.getPath().isEmpty()){
            log.warn("Path info is not existed!"+fenceVo.toString());
            return new Result(FAIL.code, false, null,"Path info is not existed!");
        }
        // 设备未上传
        if(fenceVo.getDevices()==null||fenceVo.getDevices().isEmpty()){
            log.warn("Devices info is not existed!"+fenceVo.toString());
            return new Result(FAIL.code, false, null,"Devices info is not existed!");
        }
        // 路径上传不完整
        for(List<Double> point:fenceVo.getPath()){
            if(point.size()<2){
                log.warn("Path info is incomplete!"+fenceVo.toString());
                return new Result(FAIL.code, false, null,"Path info is incomplete!");
            }
        }

        Fence fence=new Fence(fenceVo);
        System.out.println(fence.toString());
        return electricFenceServiceImpl.createFence(fence);
    }

    /**
     * 获取电子围栏
     * @param imei 设备imei号
     * @return 返回坐标点信息path
     */
    @GetMapping("/getFence")
    public Result getFence(@RequestParam(value = "imei") String imei){
        if(imei==null||imei.isEmpty()){
            log.warn("Imei is null!");
            return new Result(FAIL.code, false, null,"Device imei is not existed!");
        }
        return electricFenceServiceImpl.getFence(imei);
    }

    @PostMapping("/update")
    @ResponseBody
    public Result updateFence(@RequestBody Fence fence){

        return electricFenceServiceImpl.updateFence(fence);
    }


    @GetMapping("/getDeviceList")
    @ResponseBody
    public Result getDeviceList(){

        return new Result(SUCCESS.code,true, FenceStoreUtil.imeiToPointList,"success");  // 测试用
    }
}
